package com.example.t470p.facechecktickets.DynamicFaceMatch.view;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

import com.syn.phoneface.ui.camera.CameraFaceConfig;
import com.syn.phoneface.ui.view.CameraPreview;
import com.synface.utils.Logs.Logs;
import com.example.t470p.facechecktickets.R;

/**
 * Created by wangzhi on 2016/5/23.
 */
public class FrameFaceView extends View implements CameraPreview {

    private static final String TAG = FrameFaceView.class.getSimpleName();

    private int previewWidth = 1;
    private int previewHeight = 1;

    private Paint mPaint;

    private int[] mLocFace;

    private int mWidth = 0;

    private int mHeight = 0;

    private float mDx;

    private float mDy;

    private Bitmap mBitmap;

    private int mBitmapWidth;

    private int mBitmapHeight;

    private int mCameraId;

    private Context mContext;

    public void setCameraId(int cameraId) {
        mCameraId = cameraId;
    }

    public FrameFaceView(Context context) {
        super(context);
        init(context);
    }

    public FrameFaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FrameFaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.parseColor("#f0d701"));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(2);
        mBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.frame2)).getBitmap();
        mBitmapWidth = mBitmap.getWidth();
        mBitmapHeight = mBitmap.getHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int mTempWidth = getMeasuredWidth();
        int mTempHeight = getMeasuredHeight();

        float radio = (float) mTempWidth / mTempHeight;

        if (radio < (float) previewWidth / previewHeight) {
            mHeight = getMeasuredHeight();
            mWidth = mHeight * previewWidth / previewHeight;
        } else {
            mWidth = getMeasuredWidth();
            mHeight = mWidth * previewHeight / previewWidth;
        }
        mDx = (float) (mWidth - mTempWidth) / 2f;
        mDy = (float) (mHeight - mTempHeight) / 2f;

        Logs.i(TAG, "width:" + mWidth + " height:" + mHeight);
        Logs.i(TAG, "getLeft:" + getLeft() + "getTop:" + getTop());
        Logs.i(TAG, "dx:" + mDx + "dy:" + mDy);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mLocFace == null)
            return;

        float right;
        float left;

        if (mCameraId == CameraFaceConfig.CAMERA_FRONT) {
            right = getX(mLocFace[0]) - mDx;
            left = right - (float) mLocFace[2] * mWidth / previewWidth;
        } else {
            left = getX(mLocFace[0]) - mDx;
            right = left + (float) mLocFace[2] * mWidth / previewWidth;
        }

        float top = getY(mLocFace[1]) - mDy;
        float bottom = top + (float) mLocFace[3] * mHeight / previewHeight;

//        Logs.i(TAG, "真实x:" + mLocFace[0]);
//        Logs.i(TAG, "真实width:" + mLocFace[2]);
//        Logs.i(TAG, "mDx:" + mDx);
//        Logs.i(TAG, "mDY:" + mDy);
//        Logs.i(TAG, "left:" + left);
//        Logs.i(TAG, "right:" + right);

        canvas.drawRect(new Rect((int) left, (int) top, (int) right, (int) bottom), mPaint);
//        canvas.drawBitmap(mBitmap, new Rect(0, 0, mBitmapWidth, mBitmapHeight), new Rect((int) left, (int) top, (int) right, (int) bottom), mPaint);
    }

    private float getY(int y) {
        return (float) y * mHeight / previewHeight;
    }

    private float getX(int x) {
        if (mCameraId == CameraFaceConfig.CAMERA_FRONT) {
            return mWidth - (float) x * mWidth / previewWidth;
        } else {
            return (float) x * mWidth / previewWidth;
        }
    }

    public int[] getLocFace() {
        return mLocFace;
    }

    public void setLocFace(int[] locFace) {
        mLocFace = locFace;
        postInvalidate();
    }

    @Override
    public void onPreviewSize(int width, int height) {
        Logs.i(TAG, "width:" + width + " height:" + height);
        if (isScreenOriatationPortrait(mContext)) {
            previewWidth = height;
            previewHeight = width;
        } else {
            previewWidth = width;
            previewHeight = height;
        }

        requestLayout();
    }

    public static boolean isScreenOriatationPortrait(Context context) {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    }
}
